#include <flashOpt.h>
#include <eeprom.h>

int flashOptRead(flashAddrType flashAddr, void *obuf, int osize)
{
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    char tmpfile[128];
    int tmpsize;
    if (!obuf)
    {
        logerrorc("error:%d", osize);
        return -1;
    }
    snprintf(tmpfile, sizeof(tmpfile), "/tmp/flashChk_%u.txt", flashAddr);
    FILE *tmpFd = fopen(tmpfile, "r");
    if (!tmpFd)
    {
        logerrorc("open error %s", tmpfile);
        return -1;
    }
    tmpsize = fread(obuf, 1, osize, tmpFd);
    fclose(tmpFd);
    return tmpsize;
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_ARM32_MCU
    memset(obuf, 0xff, osize);
    return 0;
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_C51_MCU
    memset(obuf, 0xff, osize);
    eepromRead(flashAddr, obuf, osize);
    return osize;
#endif
}
int flashOptWrite(flashAddrType flashAddr, const void *ibuf, int isize)
{
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    char tmpfile[128];
    int tmpsize;
    if (!ibuf)
    {
        logerrorc("error:%d", isize);
        return -1;
    }
    snprintf(tmpfile, sizeof(tmpfile), "/tmp/flashChk_%u.txt", flashAddr);
    FILE *tmpFd = fopen(tmpfile, "w+");
    if (!tmpFd)
    {
        logerrorc("open error %s", tmpfile);
        return -1;
    }
    tmpsize = fwrite(ibuf, 1, isize, tmpFd);
    fclose(tmpFd);
    return tmpsize;
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_ARM32_MCU
    return 0;
#elif DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_C51_MCU
    eepromErase(flashAddr);
    eepromWrite(flashAddr, ibuf, isize);
    return isize;
#endif
}
